import {
  Controller,
  Get,
  Post,
  Body,
  Param,
  Delete,
  ParseIntPipe,
  Query,
} from '@nestjs/common';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiParam,
} from '@nestjs/swagger';
import { ApiResponse as CustomApiResponse, PaginationData } from '../../common/interfaces/response.interface';
import { PaginationDto } from '../../common/dto/pagination.dto';
import { UsersService } from './users.service';
import { CreateUserDto } from './dto/create-user.dto';
import { User } from './entities/user.entity';

@ApiTags('Users')
@Controller('users')
export class UsersController {
  constructor(private readonly usersService: UsersService) {}

  @Post('add')
  @ApiOperation({ summary: '添加用户' })
  @ApiResponse({
    status: 201,
    description: '用户添加成功',
    schema: {
      example: {
        code: 0,
        message: 'success',
        data: {
          id: 1,
          username: 'john_doe',
          email: 'john@example.com',
          nickname: 'John',
          avatar: null,
          isActive: true,
          createdAt: '2024-01-01T00:00:00.000Z',
          updatedAt: '2024-01-01T00:00:00.000Z'
        },
        timestamp: '2024-01-01T00:00:00.000Z'
      }
    }
  })
  @ApiResponse({
    status: 409,
    description: '用户名或邮箱已存在',
    schema: {
      example: {
        code: 409,
        message: '用户名或邮箱已存在',
        data: null,
        timestamp: '2024-01-01T00:00:00.000Z',
        path: '/api/users/add'
      }
    }
  })
  addUser(@Body() createUserDto: CreateUserDto): Promise<User> {
    return this.usersService.create(createUserDto);
  }

  @Get('get')
  @ApiOperation({ summary: '获取所有用户' })
  @ApiResponse({
    status: 200,
    description: '获取用户列表成功',
    type: [User],
  })
  getUsers(): Promise<User[]> {
    return this.usersService.findAll();
  }

  @Get('page')
  @ApiOperation({ summary: '分页获取用户列表' })
  @ApiResponse({
    status: 200,
    description: '分页获取用户列表成功',
    schema: {
      example: {
        code: 0,
        message: 'success',
        data: {
          items: [
            {
              id: 1,
              username: 'john_doe',
              email: 'john@example.com',
              nickname: 'John',
              avatar: null,
              isActive: true,
              createdAt: '2024-01-01T00:00:00.000Z'
            }
          ],
          total: 1,
          page: 1,
          pageSize: 10,
          totalPages: 1
        },
        timestamp: '2024-01-01T00:00:00.000Z'
      }
    }
  })
  getUsersPage(@Query() paginationDto: PaginationDto): Promise<PaginationData<User>> {
    return this.usersService.findAllWithPagination(paginationDto);
  }

  @Get('get/:id')
  @ApiOperation({ summary: '根据ID获取用户' })
  @ApiParam({ name: 'id', description: '用户ID' })
  @ApiResponse({
    status: 200,
    description: '获取用户成功',
    type: User,
  })
  @ApiResponse({
    status: 404,
    description: '用户不存在',
    schema: {
      example: {
        code: 404,
        message: '用户不存在',
        data: null,
        timestamp: '2024-01-01T00:00:00.000Z',
        path: '/api/users/get/1'
      }
    }
  })
  getUserById(@Param('id', ParseIntPipe) id: number): Promise<User> {
    return this.usersService.findOne(id);
  }

  @Delete('delete/:id')
  @ApiOperation({ summary: '删除用户' })
  @ApiParam({ name: 'id', description: '用户ID' })
  @ApiResponse({
    status: 200,
    description: '用户删除成功',
    schema: {
      example: {
        code: 0,
        message: 'success',
        data: null,
        timestamp: '2024-01-01T00:00:00.000Z'
      }
    }
  })
  @ApiResponse({
    status: 404,
    description: '用户不存在',
    schema: {
      example: {
        code: 404,
        message: '用户不存在',
        data: null,
        timestamp: '2024-01-01T00:00:00.000Z',
        path: '/api/users/delete/1'
      }
    }
  })
  deleteUser(@Param('id', ParseIntPipe) id: number): Promise<void> {
    return this.usersService.remove(id);
  }
}